Skip to content

Latest commit

 

History

History
41 lines (30 loc) · 1.16 KB

Getting full Github URL of the latest commit.md

File metadata and controls

41 lines (30 loc) · 1.16 KB
title created date slug publish tags
Getting full Github URL of the latest commit
2023-01-12 15:46
2023-01-13
getting-full-github-url-of-the-latest-commit
true
github
command-line

I want to share my latest commit's url, previously I would go to Github repo, click the latest commit message, then copy the url.

gh repo view -w # Opens current repo in the browser

It turns out that building the URL is much simpler

gh browse -c -n | pbcopy # Returns current url of the last commit tree, then pipe to macOS clipboard
# e.g. https://github.com/narze/advent-of-code-2022/tree/8de068f8645dbe1ba01f882f3e5326322ba07322

By the way I want to see the commit diff not the file tree, naive approach

gh browse -c -n | sed "s/\/tree\//\/commit\//" # Replaces /tree/ with /commit/

Neat! But it will cause bad URL if you ever have a repo called tree ...

So that I asked ChatGPT but...

I fallback to good ol' StackOverflow and found the ultimate solution

echo "$(gh repo view --json url --jq .url)/commit/$(git rev-parse HEAD)"